<?php
namespace app\admin\model;
use app\common\BaseModel;
use think\facade\Cookie;
use app\admin\model\Role;
// use think\model\concern\SoftDelete;
class User extends BaseModel{
    // use SoftDelete; 
    // protected $deleteTime = 'delete_time';
	 // 模型初始化
	// protected static $validate = new \app\admin\validate\User;
    protected static function init()
    {
        parent::init();
    }
   
   
    public function searchRoleIdAttr($query, $value, $data)
    {
        $query->where('role_id','<>', \app\common\BaseController::ADMIN_ID);
    }
    public function searchIDAttr($query, $value, $data)
    {
        if(!empty($value))
            $query->where('id', $value);
    }
    public function Oselect(){
        return User::withSearch(['status','role_id','id'],
            ['id'=>input('id')]
        )->find();
    }
    public function Aselect(){
        return User::withSearch(['status','role_id','id'],
            ['id'=>input('id')]
        )->select();
    }
	public function register(){

		$this->save((input()));
         return  self::$validate->getError();
	}
    public static function getRole($role_id){
        $role = new Role();
        return $role->where('id',$role_id)->find();
    }
    const private_key = "-----BEGIN PRIVATE KEY-----
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAL8c7R0a3IRhVB0A
TMuasxoUSHO0oXrDAoLkcxAeRDmgW/O4h+21dHwMDJScPz9oaUaCrMJM+Q5r92b8
JCxhGHrtSqLijoYYOMB3YRJC/7adcWH+9DQ5+OwhHNzDiq84gbcjOJ7gEZxZPVqD
djfMYy1zYZq8A3p+isH/D/1MZDMBAgMBAAECgYEAoiW0uCD2QAskBoceoRDcLKBQ
6Hq1FyclllCln2NOl2YLHjnOekyeTDEc62m17+Jyj7SHR0NvfGD2Q5ZiSpUG4q3K
AYN5lpWDBvd/c7TIBS7jadV8a47gPKe4za2EhNyt9ilf85UvXR//gu4ZzdlGMseE
TgVn6erjXW2ykbhubNECQQDiWZO8tJTkPkcDkghok7pY8lOD1SVba3NBbkGjtgRo
s+o8nmKbXUhbBkVe34zVvRuruy76slsegfsePIyGmY3fAkEA2CW2q71HCzwkujhc
6HS+C40tovaJx9QLUUiFG0pQ02/wjp6rVGZOuqAv8vj9owpFNSyEyv6ougul9svR
qdqbHwJBAKAiWXfOleun95uaC+LYaKSWw+jnuackrISk2UFV+2aLqnK6Zu6dnxvu
otNM+pkYUeQ76C+qUPaTZLJuiciMCCcCQQDP/pKm9vAbjpelH0HAaz7J7OQCko2J
XrI5WLjFxyC6JljtVSB3ii+uhSE/BX1FXEe24I00rhP9W+Ir+Hxcn8FvAkBzDRFy
/kVPLruTDogyYx7IfDSgZXnCmRN1oMNZXHoy2+qm88PyowYDFc/6SJgwKlm1Gq8u
hqVOLVIPI+uXQIHW
-----END PRIVATE KEY-----
";
    /**
     * RSA解密方法
     * @param string $encrypted
     * @return string
     */
    public static function decrypt($encrypted) {
        $pi_key =  openssl_pkey_get_private(trim(self::private_key));

        $res = openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);
       	if($res)
        	return $decrypted;
        return false;
    }

    public function login(){

            
       // if(!geetest_check(input())){
       //      return  ['msg'=>'error','info'=>'滑块还没验证'];
       // }
            $info = ['msg'=>'error','info'=>'手机号或密码错误'];

           if(!self::where('mobile',input('mobile'))->find())
                return $info;
            $user = self::where('mobile', input('mobile'))->find();
           
            if(self::decrypt($user->password) !== self::decrypt(input('password')))
            	return $info;

            if(input('remember') === '1'){
                Cookie::forever('uid',$user->id);
                Session('role_id',$user->getData('role_id'));
            }
            else{
                Cookie::set('uid',$user->id);
                session('role_id',$user->getData('role_id'));
            }
            $info = Controller('app\common\BaseController')->isAdmin ? 'admin' : '/' ;
            
            return  ['msg'=>'success','info'=>$info];
    }
}
?>